package com.hx.zhg.util;

import java.util.Iterator;
import java.util.Map;

/**
 * Created by zhenghegong on 2018/1/4.
 */
public class URLUtilExt {
    public static String jsonToUrlParam(String json) {
        Map<String, Object> map = JsonUtils.jsonToMap(json);
        return jsonToUrlParam(map);
    }

    public static String jsonToUrlParam(Map<String, Object> map) {
        StringBuilder sb = new StringBuilder();
        if (map != null && map.size() > 0) {
            Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> next = iterator.next();
                String key = next.getKey();
                String value = String.valueOf(next.getValue());
                sb.append("&");
                sb.append(key);
                sb.append("=");
                sb.append(value);
            }
            if (sb.length() > 0) {
                sb.delete(0, 1);
            }
        }
        return String.valueOf(sb);
    }


    public static void main(String[] args) {
        String json = "{\"age\":26,\"bmi\":24.221453,\"bmiMax\":24,\"bmiMin\":18.5,\"bmr\":1471.5,\"bodyAge\":26,\"boneMuscleWeight\":37.348984,\"boneMuscleWeightMax\":37.331425,\"boneMuscleWeightMin\":30.543892,\"boneWeight\":3.256705,\"boneWeightMax\":3.85,\"boneWeightMin\":3.15,\"fatControl\":0,\"fatLevel\":2,\"fatPercentage\":0.206,\"fatPercentageMax\":0.28,\"fatPercentageMin\":0.18,\"fatWeight\":14.397139,\"fatWeightMax\":19.6,\"fatWeightMin\":12.6,\"goalWeight\":0,\"height\":170,\"hepaticAdiposeInfiltration\":2,\"lbm\":55.60286,\"m_smm\":45.25021,\"muscleControl\":0,\"muscleWeight\":52.346157,\"muscleWeightMax\":49.77523,\"muscleWeightMin\":40.72519,\"physique\":6,\"proteinWeight\":10.346154,\"proteinWeightMax\":11.935,\"proteinWeightMin\":9.765,\"score\":78,\"sex\":0,\"sw\":62.41786,\"swMax\":68.659645,\"swMin\":56.176075,\"visceralFatPercentage\":5,\"visceralFatPercentageMax\":14,\"visceralFatPercentageMin\":10,\"waterPercentage\":60,\"waterWeight\":42,\"waterWeightMax\":46.2,\"waterWeightMin\":37.8,\"weight\":70,\"weightControl\":-7.58214}\n";
        String s = jsonToUrlParam(json);
        String base = "http://127.0.0.1:8899/evalua/getGuestEvaluaDetails" + "?userId=7&deviceType=1&" + s;
        System.out.println(base);
    }
}
